% 读取图像并灰度化
image = imread('pic_measure.png');
gray_image = rgb2gray(image);

% 高斯模糊
sigma = 2;
smoothed_image = imgaussfilt(gray_image, sigma);

% 边缘检测
edges_sobel = edge(smoothed_image, 'Sobel');
edges_canny = edge(smoothed_image, 'Canny');

% 保存结果
imwrite(smoothed_image, 'gaus.png');
imwrite(edges_canny, 'canny.png');
imwrite(edges_sobel, 'sobel.png');

% 获取边缘图像
edges = edge(smoothed_image, 'Sobel');

% 获取边缘像素的坐标
[y_coords, x_coords] = find(edges_canny);

% Fitted line coefficients from polyfit
p_left = polyfit(x_coords(1:round(end/2)), y_coords(1:round(end/2)), 1);
p_right = polyfit(x_coords(round(end/2):end), y_coords(round(end/2):end), 1);

% Calculate the slopes and intercepts
m_left = p_left(1);
c_left = p_left(2);
m_right = p_right(1);
c_right = p_right(2);

% Calculate the distance between the two lines
distance_between_lines = abs(c_right - c_left) / sqrt(1 + (m_left * m_right)^2);

% Output the perimeter
perimeter = 2 * distance_between_lines; % Assuming both sides are symmetrical
fprintf('外围长度（周长）：%.2f\n', perimeter);

% --show in image viewer-- %

% Load image and convert to grayscale
% image = imread('canny.png');
% gray_image = rgb2gray(image);

% Apply Sobel filter to get edges
% edges = edge(gray_image, 'Sobel');

% Get the coordinates of the edges
[y_coords, x_coords] = find(edges_canny);

% Fit lines to left and right edges
p_left = polyfit(x_coords(1:round(end/2)), y_coords(1:round(end/2)), 1);
p_right = polyfit(x_coords(round(end/2):end), y_coords(round(end/2):end), 1);

% Get the x range for plotting the lines
x_range = [min(x_coords), max(x_coords)];

% Calculate the y coordinates for the fitted lines
y_left = p_left(1) * x_range + p_left(2);
y_right = p_right(1) * x_range + p_right(2);

% Connect broken lines using linear interpolation
num_points = 100; % Number of points to connect
x_interp_left = linspace(x_range(1), x_range(2), num_points);
y_interp_left = polyval(p_left, x_interp_left);

x_interp_right = linspace(x_range(1), x_range(2), num_points);
y_interp_right = polyval(p_right, x_interp_right);

% Plot the original image
imshow(image);
hold on;

% Plot the detected edges as white dots
plot(x_coords, y_coords, 'w.', 'MarkerSize', 2);

% Plot the left and right fitted lines
plot(x_range, y_left, 'r', 'LineWidth', 2); % Left line in red
plot(x_range, y_right, 'g', 'LineWidth', 2); % Right line in green

% Plot the connected lines
plot(x_interp_left, y_interp_left, 'r--', 'LineWidth', 1); % Left line in red dashed
plot(x_interp_right, y_interp_right, 'g--', 'LineWidth', 1); % Right line in green dashed

% Mark lengths contributing to the perimeter
line1_length = sqrt((x_interp_left(end) - x_interp_left(1))^2 + (y_interp_left(end) - y_interp_left(1))^2);
line2_length = sqrt((x_interp_right(end) - x_interp_right(1))^2 + (y_interp_right(end) - y_interp_right(1))^2);

% Annotate segments between lines with length
text(mean(x_interp_left), mean(y_interp_left), sprintf('Segment 1: %.2f units', line1_length), 'Color', 'r', 'FontSize', 12, 'HorizontalAlignment', 'center');
text(mean(x_interp_right), mean(y_interp_right), sprintf('Segment 2: %.2f units', line2_length), 'Color', 'g', 'FontSize', 12, 'HorizontalAlignment', 'center');

% Annotate segments between lines
text(mean(x_range), mean(y_left), 'Segment 1', 'Color', 'w', 'FontSize', 12, 'HorizontalAlignment', 'center');
text(mean(x_range), mean(y_right), 'Segment 2', 'Color', 'w', 'FontSize', 12, 'HorizontalAlignment', 'center');

hold off;
title('Connected Broken Lines and Detected Segments');
